package com.day0921.Man_machine_guessing;

import com.day0921.Man_machine_guessing.Computer;
import com.day0921.Man_machine_guessing.User;

import java.util.Scanner;

public class Game {
    Scanner in = new Scanner(System.in);
    User user = new User();
    Computer computer = new Computer();

    public void print(){
        System.out.println("----------欢迎进入游戏世界----------");
        System.out.println("          ****************");
        System.out.println("          ***猜拳  开始***");
        System.out.println("          ****************");
        System.out.println("出拳规则：1.剪刀 2.石头 3.布");
    }

    //  获取计算机名字
    public void cRole(){
        computer.computerRole();
    }
    //  获取用户名字
    public void uRole(){
        System.out.print("请输入你的名字：");
        user.user_name = in.next();
    }

    //开始游戏
    public void action(){
        System.out.println("要开始吗？（y/n）");
        String action = in.next();

        if ("y".equals(action)){
            int count = begin();
            balance(count);
        }
        else {
            System.out.println("欢迎再来！");
        }
    }
    //循环游戏
    public int begin(){
        String answer;
        int count = 0;
        do{
            count++;
            System.out.print("请出拳：1.剪刀 2.石头 3.布（输入相应是数字）:");
            int punch = in.nextInt();
            if (punch < 1 || punch > 3){
                System.out.print("输入错误，请重新输入：");
                punch = in.nextInt();
            }
            String u_punch = user.userPunch(punch);
            System.out.println("你出拳：" + u_punch);
            String c_punch = computer.computerPunch();
            System.out.println(computer.computer_name + "出拳：" + c_punch);
            if (u_punch.equals(c_punch)){
                System.out.println("平局");
            }else if (("石头".equals(u_punch))&&("剪刀".equals(c_punch))||("剪刀".equals(u_punch))&&("布".equals(c_punch))||("布".equals(u_punch))&&("石头".equals(c_punch))){
                System.out.println("结果：恭喜，你赢了！");
                user.user_score++;
            }else {
                System.out.println("结果，抱歉，你输了！");
                computer.computer_score++;
            }
            System.out.println();
            System.out.print("是否开始下一轮（y/n）：");
            answer = in.next();
        }while ("y".equals(answer));
        return count;
    }

    //结算
    public void balance(int count){
        System.out.println("------------------------------");
        System.out.println(computer.computer_name + "VS" + user.user_name);

        System.out.println("对战次数：" + count);
        System.out.println();
        System.out.println("姓名\t\t\t得分");
        System.out.println(user.user_name + "\t\t\t" + user.user_score);
        System.out.println(computer.computer_name + "\t\t\t" + computer.computer_score);
        if (user.user_score == computer.computer_score){
            System.out.println("结局：平局！");
        }
        else if (user.user_score > computer.computer_score){
            System.out.println("结局：恭喜恭喜！");
        }
        else {
            System.out.println("结局：你输了！");
        }
    }

}
